import {Component, OnInit} from "@angular/core";
import {UserService} from "../../UserService";
import {LocalDataSource} from "ng2-smart-table";
import {BillService} from "../../energy/api";
import {BaseRequest} from "../../utils/BaseRequest";
import {UserInfo} from "../../energy/api/model/userInfo";

@Component({
  templateUrl: './user-bill.component.html'
})
export class UserBillComponent implements OnInit{

  user:UserInfo;
  settings = {
    pager:{
      display:false,
    },
    hideSubHeader: true,
    actions: {
      add:false,
      edit:false,
      delete: false,
      columnTitle: '操作',
      position: 'right',
    },
    columns: {
      billNo: {
        title: '账单编号',
        type: 'string',
      },
      billType: {
        title: '账单类型',
        type: 'string',
      },
      unitId:{
        title: '单元ID',
        type: 'number'
      },
      grossArea: {
        title: '建筑面积',
        type: 'string',
      },
      netArea: {
        title: '有效面积',
        type: 'string',
      },
      billDate: {
        title: '账单月',
        type: 'string',
      },
      billCreateDate: {
        title: '出账日期',
        type: 'string',
      },
      billStartTime: {
        title: '开始计费',
        type: 'string',
      },
      billEndTime: {
        title: '停止计费',
        type: 'string',
      },
      billLimitDate: {
        title: '应缴截止日期',
        type: 'string',
      },
      totalConsumption: {
        title: '总结算用电量',
        type: 'string',
      },
      totalPooledAmt: {
        title: '公摊金额(总)',
        type: 'string',
      },
      amt: {
        title: '金额',
        type: 'string',
      },
      totalAmt: {
        title: '结算金额',
        type: 'string',
      },
      billStatus: {
        title: '账单状态',
        type: 'string',
      },
      extInfo: {
        title: '额外信息',
        type: 'string',
      },
    },
  };

  source: LocalDataSource = new LocalDataSource();
  maxPage:number = 1;
  page:number = 1;
  limit:number = 25;
  constructor(private userService:UserService,private api:BillService){
    this.user = userService.getUser();
  }

  ngOnInit(): void {
    this.goto(this.page);
  }

  goto(page:number){
    this.page = page;
    this.api.requestECAUnitChargeBill(new BaseRequest(
      {
        pageIndex:(this.page-1)*this.limit,
        pageSize:this.limit,
        unitId:this.user.unitId.toString()
      }
  )).toPromise().then(response=>{
      console.log(response);
      this.maxPage = Math.ceil(response.dataCount / this.limit);
      this.source.load(response.data.map(value => {
        // value['device'] = `<a href="/#/pages/usr/dev">查看设备</a>`;
        return value;
      }));
    })
  }
}
